package com.glq1218.permission.controller;

import com.glq1218.domain.*;
import com.glq1218.permission.bo.AddPermissionBO;
import com.glq1218.permission.bo.PermissionBO;
import com.glq1218.permission.bo.QueryPermissionBO;
import com.glq1218.permission.bo.UpdatePermissionBO;
import com.glq1218.permission.dto.AddPermissionDTO;
import com.glq1218.permission.dto.PermissionDTO;
import com.glq1218.permission.dto.QueryPermissionDTO;
import com.glq1218.permission.dto.UpdatePermissionDTO;
import com.glq1218.permission.service.PermissionService;
import com.glq1218.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author glq1218
 * @description TODO
 * @since 2023/4/18 15:11
 */
@RestController
@RequestMapping("/api/permission")
@RequiredArgsConstructor
public class PermissionController {
    private final PermissionService permissionService;

    @GetMapping
    @PostAuthorize("hasAuthority('system:permission:list')")
    public R<PaginationResult<PermissionDTO>> queryPermissionList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryPermissionDTO queryPermissionDTO) {
        // 将DTO转为BO
        QueryPermissionBO queryPermissionBO = BeanCopyUtils.copyBean(queryPermissionDTO, QueryPermissionBO.class);

        // 调用service获取权限信息
        PaginationResult<PermissionBO> paginationResult = permissionService.queryPermissionList(paginationParam, sortParam, intervalStr, queryPermissionBO);
        // 获取BO转换为DTO
        List<PermissionDTO> permissionDTOList = new ArrayList<>();
        // 结果不为空才能进行转换
        if (Objects.nonNull(paginationResult.getRecords())) {
            List<PermissionBO> records = paginationResult.getRecords();
            permissionDTOList = BeanCopyUtils.copyBeanList(records, PermissionDTO.class);
        }
        // 返回封装好的分页查询DTO
        return R.ok(PaginationResult.<PermissionDTO>builder()
                .total(paginationResult.getTotal())
                .records(permissionDTOList)
                .build()
        );
    }

    @GetMapping("/queryAllPermission")
    public R<List<RolePermission>> queryAllPermission() {
        List<RolePermission> rolePermissionList = permissionService.queryAllPermission();
        return R.ok(rolePermissionList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @PostAuthorize("hasAuthority('system:permission:list')")
    public R<PermissionDTO> getPermissionById(@PathVariable Serializable id) {
        PermissionBO permissionBO = permissionService.getPermissionById(id);
        if (permissionBO == null) {
            return R.fail("不存在该id的权限信息");
        }
        PermissionDTO permissionDTO = BeanCopyUtils.copyBean(permissionBO, PermissionDTO.class);
        return R.ok(permissionDTO);
    }

    /**
     * 新增数据
     *
     * @param addPermissionDTO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R<?> addPermission(@RequestBody AddPermissionDTO addPermissionDTO) {
        AddPermissionBO addPermissionBO = BeanCopyUtils.copyBean(addPermissionDTO, AddPermissionBO.class);
        String result = permissionService.addPermission(addPermissionBO);
        return R.ok(result);
    }

    /**
     * 修改数据
     *
     * @param updatePermissionDTO 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R<?> updatePermission(@RequestBody UpdatePermissionDTO updatePermissionDTO) {
        UpdatePermissionBO updatePermissionBO = BeanCopyUtils.copyBean(updatePermissionDTO, UpdatePermissionBO.class);
        String result = permissionService.updatePermission(updatePermissionBO);
        return R.ok(result);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R<?> deleteByIds(@RequestParam("idList") List<Long> idList) {
        return R.ok(permissionService.removeByIds(idList));
    }

    @GetMapping("/queryMenu")
    public R<List<MenuTree>> queryMenuTree() {
        return R.ok(permissionService.queryMenuTree());
    }

    @GetMapping("/queryPMenu")
    public R<List<MenuTree>> queryTopMenuTree() {
        return R.ok(permissionService.queryTopMenuTree());
    }
}
